package com.suning.sawp.service.impl.dao.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.task.QueryRegionAssign;
import com.suning.sawp.intf.dao.task.CommTaskHqDaoService;
import com.suning.sawp.po.task.CommTaskHq;
import com.suning.sawp.po.task.CommTaskRegion;
import com.suning.sawp.service.util.BeanUtil;

@Service
public class CommTaskHqDaoServiceImpl implements CommTaskHqDaoService{

    @Resource
    DalClient dalClient;
    
    @Override
    public int queryCommCount(CommTaskHq queryCon) {
        Map<String,Object> paramMap = BeanUtil.describe(queryCon);
        Map<String, Object> numMap = dalClient.queryForMap("COMM_TASK_HQ.QUERY_COUNT_BY_CON", paramMap);
        return numMap != null ? ((Number)numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<CommTaskHq> queryCommList(CommTaskHq queryCon, Integer pageNo, Integer pageSize) {
        Map<String,Object> paramMap = BeanUtil.describe(queryCon);
        paramMap.put("startIndex", (pageNo-1)*pageSize);
        paramMap.put("maxCount", pageSize);
        return dalClient.queryForList("COMM_TASK_HQ.QUERY_BY_CON", paramMap,CommTaskHq.class);
    }

    @Override
    public boolean updateCommById(CommTaskHq comm, Integer Id) {
        Map<String,Object> paramMap = BeanUtil.describe(comm);
        paramMap.put("condition_commClassifyCode", Id);
        int num = dalClient.execute("COMM_TASK_HQ.UPDATE_BY_ID", paramMap);
        return num > 0;
    }

    @Override
    public void batchInsert(Map<String, Object>[] parmMap) {
        dalClient.batchUpdate("COMM_TASK_HQ.INSERT", parmMap);
    }

    @Override
    public void softDeleteCommById(Integer Id) {
        Map<String,Object> paramMap =new HashMap<String,Object>();
        paramMap.put("commStatus", CommTaskHq.commStatusEnum.DELETE);
        paramMap.put("condition_commClassifyCode", Id);
        dalClient.execute("COMM_TASK_HQ.UPDATE_BY_ID", paramMap);
    }

    @Override
    public void deleteCommById(Integer Id) {
        Map<String,Object> paramMap =new HashMap<String,Object>();
        paramMap.put("condition_commClassifyCode", Id);     
        dalClient.execute("COMM_TASK_HQ.DELETE_BY_ID", paramMap);
    }

    @Override
    public List<QueryRegionAssign> queryCommRegionAssign(Integer commclassifyCode,Integer taskNo) {
        Map<String,Object> paramMap =new HashMap<String,Object>();
        paramMap.put("commclassifyCode", commclassifyCode);     
        paramMap.put("taskNo", taskNo);     
        return dalClient.queryForList("COMM_TASK_HQ.QUERY_REGION_COMM_ASSIGN", paramMap,QueryRegionAssign.class);
    }

    @Override
    public List<String> queryCommIdAndDestnum(Integer taskNo) {
        Map<String,Object> paramMap =new HashMap<String,Object>();
        paramMap.put("taskNo", taskNo);     
        return dalClient.queryForList("COMM_TASK_HQ.QUERY_COMMID_DESTNUM", paramMap,String.class);
    }

    @Override
    public void batchUpdate(Map<String, Object>[] paramMaps) {
         dalClient.batchUpdate("COMM_TASK_HQ.UPDATE_HQ_COMM_BY_ID", paramMaps);
    }

    @Override
    public List<String> queryRegionCommIdAndDestnum(Integer commClassifyCode) {
        Map<String,Object> paramMap =new HashMap<String,Object>();
        paramMap.put("commClassifyCode", commClassifyCode);     
        return dalClient.queryForList("COMM_TASK_HQ.QUERY_REGION_COMMID_DESTNUM", paramMap,String.class);
    }

    @Override
    public void batchUpdateRegionComm(Map<String, Object>[] paramMaps) {
        dalClient.batchUpdate("COMM_TASK_HQ.UPDATE_REGION_COMM_BY_ID", paramMaps);
    }

    @Override
    public CommTaskHq queryCommTaskHq(Integer Id) {
        Map<String,Object> paramMap =new HashMap<String,Object>();
        paramMap.put("commClassifyCode", Id);
        return dalClient.queryForObject("COMM_TASK_HQ.QUERY_BY_CON", paramMap, CommTaskHq.class);
    }

    @Override
    public Integer queryRegionTaskNo(Integer parTaskNo, String regionCode) {
        Map<String,Object> paramMap =new HashMap<String,Object>();
        paramMap.put("parTaskNo", parTaskNo);
        paramMap.put("regionCode", regionCode);
        return dalClient.queryForObject("COMM_TASK_HQ.QUERY_REGION_TASK_NO", paramMap, Integer.class);
    }

    @Override
    public void batchInsertRegionComm(Map<String, Object>[] insertMaps) {
        dalClient.batchUpdate("COMM_TASK_HQ.INSERT_REGION_COMM", insertMaps);
    }

    @Transactional
    @Override
    public void softDeleteComm(Integer commClassifyCode) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("commClassifyCode", commClassifyCode);
        paramMap.put("commStatus", CommTaskHq.commStatusEnum.DELETE);
        dalClient.execute("COMM_TASK_HQ.SOFT_DELETE_HQ", paramMap);
        dalClient.execute("COMM_TASK_HQ.SOFT_DELETE_REGION", paramMap);
        dalClient.execute("COMM_TASK_HQ.SOFT_DELETE_STORE", paramMap);
        dalClient.execute("COMM_TASK_HQ.SOFT_DELETE_CLERK", paramMap);
    }

    @Override
    public Integer insertCommTask(CommTaskHq commTask) {
        return dalClient.persist(commTask).intValue();
    }

    @Override
    public Integer insertCommRegion(CommTaskRegion commRegion) {
        return dalClient.persist(commRegion).intValue();
    }

    @Override
    public void batchUpdateById(Map<String, Object>[] paramMaps) {
        dalClient.batchUpdate("COMM_TASK_HQ.UPDATE_BY_ID", paramMaps);
    }

    @Override
    public void updateRegionTaskStatus(Integer status,Integer taskNo) {
        Map<String,Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskNo", taskNo);
        paramMap.put("assignStatus", status);
        dalClient.execute("COMM_TASK_HQ.UPDATE_REGION_BY_ID", paramMap);
    }

    @Override
    public boolean isCommRegionExist(Integer regionTaskNo, Integer commClassifyCode) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("taskNo", regionTaskNo);
        paramMap.put("commClassifyCode", commClassifyCode);
        Map<String, Object> numMap = dalClient.queryForMap("COMM_TASK_HQ.QUERY_COMM_REGION_COUNT", paramMap);
        return ((Number)numMap.get("NUM")).intValue() > 0;
    }

    @Override
    public boolean isAssignAllComm(Integer taskNo) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("taskNo", taskNo);
        paramMap.put("commStatus", CommTaskHq.commStatusEnum.NORMAL);
        paramMap.put("destNum", 0);
        Map<String, Object> numMap = dalClient.queryForMap("COMM_TASK_HQ.QUERY_COUNT_BY_CON", paramMap);
        return ((Number)numMap.get("NUM")).intValue() == 0;
    }

}
